# -*- coding: utf-8 -*-
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TuringClass.settings')
import django
from app_TuringClass.models import *
from app_accounts.models import *
from django.contrib.auth.models import User
import datetime
## author：houchaoqun
## function：初始化用户数据库
## date：2016/11/10

def Population():
	### UserID, PassWord, RoleTypeID, NickName
	AddNewUsers('23020161153319', '123456', 'student', '学生_hcq')
	AddNewUsers('hcq001', '123456', 'admin', '管理员_hcq')
	AddNewUsers('ljy001', '654321', 'admin', '管理员_ljy')
	AddNewUsers('dsy001', '654321', 'admin', '管理员_dsy')
	AddNewUsers('weijie001', '654321', 'admin', '管理员_wj')
	AddNewUsers('mxy001', '654321', 'admin', '管理员_mxy')
	AddNewUsers('xlj001', '654321', 'admin', '管理员_xlj')
	AddNewUsers('lls001', '654321', 'admin', '管理员_lls')
	AddNewUsers('liao001', '123456', 'admin', '管理员_廖老师')
	AddNewUsers('Volunteer1', '123456', 'student', '志愿者_1')
	AddNewUsers('Volunteer2', '123456', 'student', '志愿者_2')
	AddNewUsers('Volunteer3', '123456', 'student', '志愿者_3')
	AddNewUsers('Volunteer4', '123456', 'student', '志愿者_4')
	AddNewUsers('Volunteer5', '123456', 'student', '志愿者_5')
	AddNewUsers('Volunteer6', '123456', 'student', '志愿者_6')
	AddNewUsers('Volunteer7', '123456', 'student', '志愿者_7')


	### RoleTypeID, RoleTypeName, DeletePermission, PostPermission, SuperPermission
	AddNewRoleType('admin', '管理员', True, True, True)
	AddNewRoleType('student', '学生用户', True, True, False)
	AddNewRoleType('tourist', '游客用户', False, False, False)


def AddNewUsers(UserID, PassWord, RoleTypeID, NickName):
	print "[UserID] = ",UserID
	if TuringUser.objects.filter(UserID = UserID).exists():
		print "[User has been existed...]"
	else:
		NewUser = User.objects.create_user(username = UserID, password = PassWord)
		NewTuringUser = TuringUser.objects.get_or_create(CurrentTime=timezone.now(),LastLoginTime=timezone.now(),Users = NewUser, UserID = UserID, RoleTypeID = RoleTypeID, NickName = NickName)[0]
		NewTuringUser.save()
		print "-- function [AddNewUsers] successful... "

def AddNewRoleType(RoleTypeID, RoleTypeName, DeletePermission, PostPermission, SuperPermission):
	if TuringRoleType.objects.filter(RoleTypeID = RoleTypeID).exists():
		print "[RoleTypeID] = ",RoleTypeID + ", has been existed!"
	else:
		print "Table [TuringRoleType] RoleTypeID = ", RoleTypeID
		_TuringUser = TuringRoleType.objects.get_or_create(RoleTypeID = RoleTypeID, RoleTypeName = RoleTypeName, DeletePermission = DeletePermission, PostPermission = PostPermission, SuperPermission = SuperPermission)[0]
		_TuringUser.save()


### main program
if __name__ == '__main__':
	print 'DatabaseInit.py - running...'
	Population()
